package plot;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Evangelista
 */
public class PlotDraw extends javax.swing.JPanel {
    
    private int gap_x  = 0;
    private int gap_y  = 0;
    
    private int zoom_x = 2500000;
    private int zoom_y = 1000000;

    public PlotDraw() {
        this.setSize(600, 600);
        this.setVisible(true);
        
        initComponents();
    }
    
     public static void salvarMapa(BufferedImage mapa) throws IOException {
        File arq = new File("temp/mapaFinal.png");
        ImageIO.write(mapa, "png", arq);
    }
    
    @Override
    public void paintComponent( Graphics g ){
        super.paintComponent(g);
        
        URL url;
        try {
            url = new URL( GoogleMap.generate_final_map_url( 20, 640 ) );
            System.out.println(GoogleMap.generate_final_map_url( 20, 640 ));
            try {
                BufferedImage mapa = ImageIO.read(url);
                
                salvarMapa(mapa);
                
                g.drawImage(mapa, 0, 0, this);
            } catch (IOException ex) {
                Logger.getLogger(PlotGoogleMaps.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(PlotGoogleMaps.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
//        double x_ref = PlotDataSet.get(0).get_latitude();
//        double y_ref = PlotDataSet.get(0).get_longitude();
//        
//        
//        int x;
//        int y;
//        int x_novo;
//        int y_novo;
//        
//        for( int i = 0; i < PlotDataSet.size(); i++ ){
//            
//            // Calcula diferenca do ponto de referência
//            // // Multiplica por zoom para aproximar
//            x = (int)( ( x_ref - PlotDataSet.get(i).get_latitude() ) * zoom_x ) + 400;
//            y = (int)( ( y_ref - PlotDataSet.get(i).get_longitude() ) * zoom_y ) + 300;
//            
//            x += gap_x;
//            y += gap_y;
//            
//            System.out.println( x + "  -  " + y );
//            
//            if( PlotDataSet.get(i).is_winner() ){
//                g.setColor(Color.GREEN);
//            } else {
//                g.setColor(Color.RED);
//            }
//            g.fillOval(x, this.getHeight() - y - 10, 10, 10);
//            g.setColor(Color.BLACK);
//            g.drawString(PlotDataSet.get(i).get_pano_id(), x, this.getHeight() - (y+25));
//            
//            // Verifica vizinhos
//            for( int j = 0; j < PlotDataSet.get(i).get_vizinhos().size(); j++ ){
//                if( PlotDataSet.get(PlotDataSet.get(i).get_vizinhos().get(j) ) != null ){
//                    x_novo = (int)( ( x_ref - PlotDataSet.get(PlotDataSet.get(i).get_vizinhos().get(j)).get_latitude() ) * zoom_x ) + 400;
//                    y_novo = (int)( ( y_ref - PlotDataSet.get(PlotDataSet.get(i).get_vizinhos().get(j)).get_longitude() ) * zoom_y ) + 300;
//                    g.drawLine(x+5, this.getHeight() - (y+5), x_novo+5, this.getHeight() - (y_novo+5));
//                }
//            }
//            
//            // Desenha direção
//            double alpha =  -(PlotDataSet.get(i).get_grau_direcao() - (Math.PI / 2));
//            g.drawLine(x+5, this.getHeight() - (y+5), ( x+5 + (int)( 25*Math.cos(alpha) ) ), this.getHeight() - ( y+5 + (int)( 25*Math.sin(alpha) ) ));
//            
//        }
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
